//
//  JFBRegionDS.m
//  ios-supplier
//
//  Created by ccu on 15/9/11.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBRegionDS.h"

@implementation JFBRegionDS

-(instancetype)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.ID = [[attributes objectForKey: @"id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"id"] integerValue];
    self.level = [[attributes objectForKey: @"level"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"level"] integerValue];
    self.name = [[attributes objectForKey: @"name"] isEqual: [NSNull null]] ? @"" : [attributes objectForKey: @"name"];
    self.parent_id = [[attributes objectForKey: @"parent_id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"parent_id"] integerValue];
    
    return self;
}

+ (NSURLSessionDataTask *)region:(void (^)(JFBRegionDS *orderDetailInfo, NSError *error))block cityID:(NSDictionary *)cityID
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject: cityID options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *shopInfoS = [[NSString alloc] initWithData: jsonData encoding:NSUTF8StringEncoding];
    NSDictionary * parameters = @{@"shopSoInfo": shopInfoS};
    
    return [[JFBAPIClient sharedClient: YES] GET: @"/shop/purchase/so/add.json" parameters: parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        NSLog(@"%@", JSON);
        
        if( error == [NSNull null] || [error.description isEqualToString:@""])
        {
            JFBRegionDS *orderDetailInfo = [[JFBRegionDS alloc] initWithAttributes: [JSON objectForKey: @"data"]];
            block(orderDetailInfo, nil);
        }
        else
        {
            block(nil, nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}




@end
